<template>
  <div class="course-introduce">
    <div class="course-left">
      <div class="course-box">
        <div class="header">{{ courseInfo.name }}</div>
        <div class="course-item">
          <template v-for="(key, i) in Object.keys(courseInfo)">
            <div v-if="courseDetailMap[key]" :key="i" class="content-list">
              <div class="title">{{ courseDetailMap[key] }}</div>
              <div v-if="key === 'startTime' || key === 'endTime'" class="content">{{ new Date(courseInfo[key]) | parseTime('{y}-{m}-{d}') }}</div>
              <div v-else-if="key === 'column'" class="content">{{ courseInfo[key].channelName }}</div>
              <div v-else-if="key === 'category'" class="content">{{ courseInfo[key].categoryName }}</div>
              <div v-else class="content">{{ courseInfo[key] }}</div>

            </div>
          </template>
        </div>
      </div>
    </div>
    <div class="course-right">
      <div ref="comment" class="comment" />
    </div>
  </div>
</template>

<script>
import { courseDetailMap } from '@/utils/dictionary'
import { parseTime } from '@/utils'

export default {
  filters: {
    parseTime
  },
  props: {
    courseInfo: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      courseDetailMap, // 课程字典
      commentOptions: {
        legend: {
          data: ['用户评价数量']
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c} ({d}%)'
        },
        series: [
          {
            name: '用户评价数量',
            type: 'pie',
            radius: '55%',
            data: [],
            textStyle: {
              color: 'rgba(0, 0, 0, 0.6)'
            },
            labelLine: {
              lineStyle: {
                color: 'rgba(0, 0, 0, 0.3)'
              }
            }
          }
        ]
      }
    }
  },
  mounted() {
    this.getCommentData()
  },
  methods: {
    async getCommentData() {
      const { courseId } = this.$route.query
      const res = await this.$api.getCourseComment(courseId)
      if (res && res.code === 200) {
        const grade = res.data.grade
        const options = []
        Object.keys(grade).forEach(key => {
          if (grade[key] > 0) {
            const dict = {
              name: key,
              value: grade[key]
            }
            options.push(dict)
          }
        })
        this.commentOptions.series[0].data = options
        this.initCommentChart()
      }
    },
    initCommentChart() {
      const commentChart = this.$echarts.init(this.$refs.comment)
      commentChart.setOption(this.commentOptions)
      window.addEventListener('resize', () => {
        commentChart.resize()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.course-introduce{
  display: flex;
}
.course-left{
  position: relative;
  top: 50%;
  transform: translateY(20%);
  flex: 3;
  font-size: 12px;
}
.course-box{
  width: 260px;
  background-color: #f5f7fa;
  border: 1px solid #fff;
  border-radius: 4px;
  overflow: hidden;
  .header{
    overflow: hidden;
    text-overflow:ellipsis;
    white-space: nowrap;
    height: 30px;
    line-height: 30px;
    padding-left: 10px;
    font-weight: bold;
    font-size: 15px;
    background: #e4e7ed;
  }
  .content-list{
    display: flex;
    justify-content: space-between;
    background: #fff;
    padding: 5px;
    margin: 0 10px;
    border-radius: 4px;
    box-shadow: 0 2px 4px 0 rgb(0 0 0 / 10%);
    margin-top: 8px;
    &:last-child{
      margin-bottom: 8px;
    }
  }
}
.course-right{
  flex: 5;
  position: relative;
  top: 50%;
  transform: translateY(25%);
}
.comment{
  width: 400px;
  height: 300px;
}
</style>
